// pages/orderDetail/orderDetail.js
import api from "../../api/api"
Page({
  /**
   * 页面的初始数据
   */
  data: {
    message:"",
    imgurl:"",
    value:"123",
    status:"",//已签收
    orderid:0,
    receiptList:[],
    orderlist:[],
    usereval:[],
    userevallist:{
      shippingOrderNumber:""
    },
    logisticsList:[],
    type:"",
    receiptFileAddress:"",
    steps: []
  },
  //获取物流信息
  getlogist() {
    let that = this
    let i=0
    let id=wx.getStorageSync('id')
    api.post('/app/logistics/getLogisticsInfo', {shippingOrderNumber: that.data.userevallist.shippingOrderNumber,id:id})
      .then(res => {
        that.setData({
          logisticsList:res.data
        })
        res.data.map(item=>{
          that.setData({
           [`steps[${i}].desc`]:item.createTime,
           [`steps[${i}].text`]:item.address
          })
          i=i+1
        })
        // that.setData({
        //   'steps.text':item.address
        // })
      })
  },
  previewImages() {
    let that=this
    wx.previewImage({
      current:that.data.imgurl, // 当前显示图片的链接/路径
      urls: [
        that.data.imgurl
      ] // 需要预览的图片链接/路径列表
    });
  },
//获取订单详情
  getorderlist() {
    // 改变this指向
    let that = this;
    api.post('/app/logistics/getOrderDetail', that.data.userevallist)
    .then(res => {
      that.setData({
        orderlist:res.data,
      })
      if(res.data.shippingStatus=="reviewed"){
      that.setData({
        status:"已评价",
      })
      }else if(res.data.shippingStatus=="delivered"){
        that.setData({
          status:"已签收",
        })
      }else if(res.data.shippingStatus=="arrived"){
        that.setData({
          status:"已到货",
        })
      }else if(res.data.shippingStatus=="transit"){
        that.setData({
          status:"运输中",
        })
      }else if(res.data.shippingStatus=="waiting"){
        that.setData({
          status:"待发货",
        })
      }
    })
    .catch(err => {
    // wx.showToast({
    //   icon:'none',
    //   title: err.message,
    //   duration: 2000
    // })
  })
},
//回执单回显
getreceipt(){
  let that = this;
  api.post('/app/receiptOrder/getReceiptOrder', {shippingOrderNumber:that.data.userevallist.shippingOrderNumber})
  .then(res => {
    that.setData({
      receiptList:res.data,
      imgurl:"https://yunsrm.hien.cn/"+"/file/"+res.data.receiptFileAddress
    })
  })
  .catch(err => {
  // wx.showToast({
  //   icon:'none',
  //   title: err.message,
  //   duration: 2000
  // })
})
},
//获取评价详情
getuserevallist() {
  // 改变this指向
  let that = this;
  api.post('/app/evaluation/getEvaluation', that.data.userevallist)
  .then(res => {
    that.setData({
      usereval:res.data,
      message:res.data.commentContent
    })
  })
  .catch(err => {
  // wx.showToast({
  //   icon:'none',
  //   title: err.message,
  //   duration: 2000
  // })
})
},
//到货通知
noticeArrival(){
  let that = this;
  api.post('/app/logistics/arrivalNotice', {shippingOrderNumber: that.data.userevallist.shippingOrderNumber})
  .then(res => {
    if (res.code == 200){
      wx.showToast({
        icon:'none',
        title: "通知成功",
        duration: 2000
      })
    that.getorderlist()
    }else{
      wx.showToast({
        icon:'none',
        title: "通知失败",
        duration: 2000
      })
    }

  })
  .catch(err => {
  // wx.showToast({
  //   icon:'none',
  //   title: err.message,
  //   duration: 2000
  // })
})
},
//收取货物
getgoods(){
  let that = this;
  let companyid = wx.getStorageSync('companyid')
  let id = wx.getStorageSync('id')
  api.post('/companyInfoApp/updateStatus', {id:id,companyId:companyid,shippingOrderNumber: that.data.userevallist.shippingOrderNumber})
  .then(res => {
    if (res.code == 200){
      wx.showToast({
        icon:'none',
        title: '取货成功',
        duration: 2000
      })
        that.getorderlist()
        that.getlogist()
    }else{
      wx.showToast({
        icon:'none',
        title: '取货失败',
        duration: 2000
      })
    } 
  
  })
  .catch(err => {
  // wx.showToast({
  //   icon:'none',
  //   title: err.message,
  //   duration: 2000
  // })
})
},
  onLoad(options) {
    let that=this
    let type= wx.getStorageSync('type')
    that.setData({
      'userevallist.shippingOrderNumber':options.shippingOrderNumber,
      type:type
    })

  },
  onShow() {
    let that=this
    that.getorderlist()
    that.getuserevallist()
    that.getlogist()
    that.getreceipt()
  },
  onClickLeft() {
    wx.navigateBack()
  },
  goTouser(){
    let that=this
    wx.navigateTo({
      url: '/pages/userEvaluation/userEvaluation?shippingOrderNumber='+that.data.userevallist.shippingOrderNumber
    })
  },
  goToreceipt(){
    let that=this
    wx.navigateTo({
      url: '/pages/receipt/receipt?shippingOrderNumber='+that.data.userevallist.shippingOrderNumber
    })
  },
  goToupdatelog(){
    let that=this
    wx.navigateTo({
      url: '/pages/updateLogistics/updateLogistics?shippingOrderNumber='+that.data.userevallist.shippingOrderNumber
    })
  },
})